<?php

namespace app\subscribe\room;


use think\Event;
use think\facade\Db;
use app\common\repositories\room\RoomLogRepository;
use app\common\repositories\room\RoomPictureRepository;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\contract\ContractBasicRepository;
use app\common\repositories\property\basic\PropertyLogRepository;

class RoomSubscribe
{

    /**
     * 房间创建
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-05-10 15:10
     */
    public function onCreateRoom($data)
    {
        $roomInfo = $data['roomInfo'] ?? '';
        $userId = $data['userId'] ?? 0;
        $remark = $data['remark'] ?? '';

        /** @var RoomLogRepository $roomLogRepository */
        $roomLogRepository = app()->make(RoomLogRepository::class);

        $roomLogData['room_id'] = $roomInfo['id'];
        $roomLogData['property_id'] = $roomInfo['property_id'];
        $roomLogData['remark'] = $remark . '创建房间';
        $roomLogRepository->addData($roomInfo['items_id'], $roomLogData, $userId);
    }

    /**
     * 房间编辑
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-05-10 15:10
     */
    public function onRedact($data)
    {
        $roomInfo = $data['roomInfo'] ?? '';
        $userId = $data['userId'] ?? 0;
        $remark = $data['remark'] ?? '';

        /** @var RoomLogRepository $roomLogRepository */
        $roomLogRepository = app()->make(RoomLogRepository::class);

        $roomLogData['room_id'] = $roomInfo['id'];
        $roomLogData['property_id'] = $roomInfo['property_id'];
        $roomLogData['remark'] = $remark . '编辑房间';
        $roomLogRepository->addData($roomInfo['items_id'], $roomLogData, $userId);
    }


    /**
     * 监听房间删除
     * @param $data
     */
    public function onDelete($data)
    {

        $roomInfo = $data['roomInfo'] ?? '';
        $userId = $data['userId'] ?? '0';

        /** @var PropertyLogRepository $propertyLogRepository */
        $propertyLogRepository = app()->make(PropertyLogRepository::class);
        $propertyLogData['property_id'] = $roomInfo['property_id'];
        $propertyLogData['remark'] = $roomInfo['room_name'] . ' 删除';
        $propertyLogRepository->addData($roomInfo['items_id'], $propertyLogData, $userId);

        # 删除图片记录
        /** @var RoomPictureRepository $pictureRepository */
        $pictureRepository = app()->make(RoomPictureRepository::class);
        $pictureRepository->removePicture($roomInfo['id']);

        # 删除日志记录
        /** @var RoomLogRepository $roomLogRepository */
        $roomLogRepository = app()->make(RoomLogRepository::class);
        $roomLogRepository->delByRoomId($roomInfo['id']);

        # 清除能源设备
        /** @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $energyMeterRepository->unbindRoomIdEnergyMeterId($roomInfo['id']);
    }


    /**
     * 房间出租
     * @param $data
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-10 15:10
     */
    public function onRentRoom($data)
    {
        $roomId = $data['roomId'] ?? 0;
        $userId = $data['userId'] ?? 0;
        $contractId = $data['contractId'] ?? 0;

        /** @var ContractBasicRepository $contractBasicRepository */
        $contractBasicRepository = app()->make(ContractBasicRepository::class);
        $contractInfo = $contractBasicRepository->get($contractId);

        /** @var RoomLogRepository $roomLogRepository */
        $roomLogRepository = app()->make(RoomLogRepository::class);

        $roomLogData['room_id'] = $roomId;
        $roomLogData['property_id'] = $contractInfo['property_id'];
        $roomLogData['remark'] = '关联租赁编号: ' . $contractInfo['number'];
        $roomLogRepository->addData($contractInfo['items_id'], $roomLogData, $userId);

    }


    /**
     * 同未到期，提前解除关联房间
     * @param $data
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-05-10 15:11
     */
    public function onRelieveRoom($data)
    {
        $roomId = $data['roomId'] ?? 0;
        $userId = $data['userId'] ?? 0;
        $contractId = $data['contractId'] ?? 0;

        /** @var ContractBasicRepository $contractBasicRepository */
        $contractBasicRepository = app()->make(ContractBasicRepository::class);
        $contractInfo = $contractBasicRepository->get($contractId);

        /** @var RoomLogRepository $roomLogRepository */
        $roomLogRepository = app()->make(RoomLogRepository::class);
        $roomLogData['room_id'] = $roomId;
        $roomLogData['property_id'] = $contractInfo['property_id'];
        $roomLogData['remark'] = $contractInfo['number'] . ' 未到期，提前解除房间';
        $roomLogRepository->addData($contractInfo['items_id'], $roomLogData, $userId);
    }

    public function subscribe(Event $event)
    {
        $event->listen('room.create', [$this, 'onCreateRoom']);## 创建房间
        $event->listen('room.redact', [$this, 'onRedact']); ## 房间编辑
        $event->listen('room.delete', [$this, 'onDelete']);## 电表删除
        $event->listen('room.rent', [$this, 'onRentRoom']);## 房间出租
        $event->listen('room.relieve', [$this, 'onRelieveRoom']);## 房间退租
    }
}